<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /* 1、封装一个函数返回字符串的长度
            给定一个字符串如：“abaasdffggghhjjkkgfddsssss3444343”问题如下：?
         * console.log(strLength(str)) // 33
         */
        {
            function strLength(instr) {
                return instr.length;
            }
            let str = "abaasdffggghhjjkkgfddsssss3444343";
            console.log(strLength(str)); //33
            console.log(strLength("zhang")); //5
        }
        /* 2、已知有字符串foo=”get-element-by-id”
         * 写一个function将其转化成驼峰表示法”getElementById”
         * console.log(camelCase('get-element-by-id')) // getElementById
         */
        {
            function changecode(data) {
                let arr2 = []
                let arr = data.split("-")
                // for (let i = 0; i < arr.length; i++) {
                //     arr2.push = arr[i].replace(arr[i].charAt(0), arr[i].charAt(0).toUpperCase())
                // }
                arr.forEach(item => {
                    arr2.push(item.replace(item.charAt(0), item.charAt(0).toUpperCase()))
                })
                arr2[0] = arr2[0].replace(arr2[0], arr[0])
                let str = arr2.join("")
                return str
            }
            let foo = "get-element-by-id"
            console.log(changecode(foo));

        }

        /*3、请编写一个 JavaScript 函数 parseQueryString，它的用途是把 URL 参数解析为一个对象，如：
        var url = “http://www.taobao.com/index.html?key0=0&key1=1&key2=2”
        var obj = parseQueryString(url)
        console.log(obj.url)  //  http://www.taobao.com/index.html
        console.log(obj.key0) //  0
        console.log(obj.key1) //  1
        console.log(obj.key2) //  2
        */
        {
            function parseQueryString(url) {
                let arr = url.split("?")
                let arr2 = arr[1].split("&")
                let arr3 = []
                let arr4 = []
                arr2.forEach(item => {
                    arr3.push(item.split("="))
                })
                arr3.forEach(item => {
                    item.forEach(item => {
                        arr4.push(item)
                    })
                })
                arr4.unshift(arr[0])
                arr4.unshift("url")
                let obj = {}
                for (let i = 0; i < arr4.length; i += 2) {
                    obj[arr4[i]] = arr4[i + 1]
                }
                return obj
            }

        }
        let url = "http://www.taobao.com/index.html?key0=0&key1=1&key2=2";
        var obj = parseQueryString(url)
        console.log(obj);
        console.log(obj.url)
        console.log(obj.key0)
        console.log(obj.key1)
        console.log(obj.key2)


        //4、封装一个函数实现对当前时间格式化 要求月,日小于10在前面补0 2019-05-08  2019-12-01
        //var dt = new Date()
        //console.log(formatDate(dt)) // 2022-09-09
        {
            function fn(dt) {
                var year = dt.getFullYear()
                var month = dt.getMonth() + 1
                var day = dt.getDay()
                if (month < 10) {
                    month = "0" + month
                }
                if (day < 10) {
                    day = "0" + day
                }
                return `${year}-${month}-${day}`
            }
            var dt = new Date()
            console.log(fn(dt));
        }

        /* 
            5、 封装一个函数，专门用于数组去重
            let arr = ['a', 'b', 'a', 'c', 'a', 'b', 'd', 'e'];
            console.log(getOnly(arr))      // ['a', 'b', 'c', 'd', 'e']
        */
        {
            function fn(data) {
                let arr = []
                data.forEach(item => {
                    /* indexOf1111 */
                    if (arr.indexOf(item) == -1) {
                        arr.push(item)
                    }
                    /* includes2222 */
                    // if (!arr.includes(item)) {
                    //     arr.push(item)
                    // }
                })
                return arr
            }
            let arr = ['a', 'b', 'a', 'c', 'a', 'b', 'd', 'e'];
            console.log(fn(arr));


        } {
            //6. 寻找最长的单词的长度
            //返回提供的句子中最长的单词的长度。返回值应该是一个数字。
            /* 
              function getLongCount(str) {
                ...
              } 
                console.log(getLongCount("The quick brown fox jumped over the lazy dog")); //6
            */
            function getLongCount(str) {
                let arr = str.split(" ")
                let arr2 = []
                arr.forEach(item => {
                    arr2.push(item.length)
                })

                return Math.max(...arr2)
            }
            console.log(getLongCount("The quick brown fox jumped over the lazy dog"))
        }
    </script>
</body>

</html>